<?php


$path = '/data/wwwroot/static.images/words/英文释义';
$file = "/data/logs/words.bak.log";

try {
    $instance = new CFileSystem($path);
    $writeInstance = new CFileSystem($file, 'r+');

    $list = $instance->scan();

    $words = [];
    foreach ($list as $file) {
        if (is_file($file)) {
            $extension = pathinfo ($file, PATHINFO_EXTENSION);
            if ($extension !== 'xlsx') continue;
            $rows = $instance->readXlsx($file);
            echo $file . " : " . count($rows), PHP_EOL;
            foreach ($rows as $row) {
                if (empty($row[0])||empty($row[1])) continue;
                $word = trim($row[0]);
                $explain = str_replace(PHP_EOL, "\t", trim($row[1]));
                if (empty($word)) continue;
                if (empty($explain)) continue;

                if (!isset($words[$word]) || !in_array($explain, $words[$word])) {
                    $words[$word][] = $explain;
                }
            }
        }
    }

    $separator = '|||';
    $separator = '$$$';
    foreach ($words as $key => $explain) {
        $c = implode('|||', [$key, implode($separator, $explain)]) . PHP_EOL;
        $writeInstance->write($c);
    }

} catch (Exception $e) {

    print_r($e->getMessage());
}